const baseUrl = 'http://30.1.2.54:8011';
import store from '@/store/index.js';

//发起请求
export function getRequest(method,url,data,token){
	return new Promise((resolve,reject)=>{
        switch(method){
            case "get":
                break;
            case "post":
                uni.request({
                    url: baseUrl+url, 
                    method:'POST',
                    header: {
                        'content-type': 'application/json', //自定义请求头信息
                        'tokens':token,
                    },
                    data:data,
                    success: (res) => {
                        if(res.data.sate === 200){
                            resolve(res.data.data);
                        }else if(res.data.sate === 201){  //登陆超时
                            uni.showToast({
                                title: '登陆超时！',
                                duration: 1500,
                            });
                            uni.reLaunch({ //跳转到登陆页面
                                url: '/pages/login/login',
                            });
                            reject(res.data.msg);
                        }else{
                            reject(res.data.msg);
                        }
                    },
                    fail(){
                        reject("请求报错 | 未知错误");
                    },
                });
                break;
        }
	}).catch(msg=>{  //全局错误处理
        return Promise.reject(msg);
    });
};

//所有API
/**
登陆管理
**/
//用户登陆
export function login(data){
    return getRequest(
        'post',
        '/login',
        data,
    );
}
/**
病历管理
**/
//添加病历
export function addBingLi(data){
    return getRequest(
        'post',
        '/bingli/add',
        data,
        store.state.user.token,
    );
}
//获取病历详情列表
export function getBingLisInfo(data){
    return getRequest(
        'post',
        '/bingli/getBingLi',
        data,
        store.state.user.token,
    );
}
//查询病历列表
export function findBingLis(data){
    return getRequest(
        'post',
        '/bingli/list',
        data,
        store.state.user.token,
    );
}
/**
就诊卡管理
**/
//添加就诊卡
export function addCard(data){
    return getRequest(
        'post',
        '/idcard/add',
        data,
        store.state.user.token,
    );
}
//根据kid(卡号)获取病人信息
export function getPatientByKid(kid){
    return getRequest(
        'post',
        '/idcard/get',
        kid,
        store.state.user.token,
    );
}
//查询就诊卡
export function findCards(data){
    return getRequest(
        'post',
        '/idcard/list',
        data,
        store.state.user.token,
    );
}
//获取已结账用户数据
export function getPayUpUsers(){
    return getRequest(
        'post',
        "/idcard/getSuccessUser",
        undefined,
        store.state.user.token,
    );
}
//获取就诊卡列表
export function getCards(){
    return getRequest(
        'post',
        "/idcard/getCardInfo",
        undefined,
        store.state.user.token,
    );
}
//根据kid获取就诊卡基本信息
export function getCardInfoByKid(kid){
    return getRequest(
        'post',
        "/idcard/getByBase",
        kid,
        store.state.user.token,
    );
}
/**
买单管理
**/
//获取未买单的用户详细数据
export function getNoPayUser(data){
    return getRequest(
        'post',
        "/maidan/nopayUser",
        data,
        store.state.user.token,
    );
}
//结账
export function payUp(data){
    return getRequest(
        'post',
        "/maidan/add",
        data,
        store.state.user.token,
    );
}
/**
住院管理
**/
//住院统计
export function addZhuYuanTongJi(data){
    return getRequest(
        'post',
        "/zhuYuanTongJi/add",
        data,
        store.state.user.token,
    );
}
/**
挂号管理
**/
//添加挂号
export function addRegister(data){
    return getRequest(
        'post',
        "/register/add",
        data,
        store.state.user.token,
    );
}
//更新挂号
export function updateRegister(data){
    return getRequest(
        'post',
        "/register/update",
        data,
        store.state.user.token,
    );
}
//获取未付费的病人
export function getNoPayUsers(data){
    return getRequest(
        'post',
        "/register/getNoPayUsers",
        data,
        store.state.user.token,
    );
}
//获取最新的挂号编号
export function getTopRid(){
    return getRequest(
        'post',
        "/register/getTopRid",
        undefined,
        store.state.user.token,
    );
}
//获取所有的挂号单
export function getAllRgisters(){
    return getRequest(
        'post',
        "/register/getRgister",
        undefined,
        store.state.user.token,
    );
}
//根绝Rid获取挂号单数据
export function getRgisterByRid(rid){
    return getRequest(
        'post',
        "/register/getRgister",
        {
        	rid,
        },
        store.state.user.token,
    );
}
//查询挂号单
export function findRgisters(data){
    return getRequest(
        'post',
        "/register/list",
        data,
        store.state.user.token,
    );
}
/**
科室管理
**/
//查询科室
export function findSectionRooms(param){
    return getRequest(
        'post',
        "/sectionRoom/list",
        param,
        store.state.user.token,
    );
}
//查询科室子科目
export function findSectionRoomSuns(param){
    return getRequest(
        'post',
        "/section/list",
        param,
        store.state.user.token,
    );
}
/**
开药管理
**/
//添加开药信息
export function addPrescribe(param){
    return getRequest(
        'post',
        "/kaiyao/add",
        param,
        store.state.user.token,
    );
}
//获取已结账用户的药单
export function getPayUpDrugs(param){
    return getRequest(
        'post',
        "/kaiyao/getSuccessYao",
        param,
        store.state.user.token,
    );
}
/**
药品管理
**/
//添加药品
export function addDrug(param){
    return getRequest(
        'post',
        "/drug/add",
        param,
        store.state.user.token,
    );
}
//查询药品
export function findDrugs(param){
    return getRequest(
        'post',
        "/drug/list",
        param,
        store.state.user.token,
    );
}
//删除药品
export function deleteDrug(param){
    return getRequest(
        'post',
        "/drug/delete",
        param,
        store.state.user.token,
    );
}
//修改药品
export function updateDrug(param){
    return getRequest(
        'post',
        "/drug/update",
        param,
        store.state.user.token,
    );
}
//根据Rid获取病人药单
export function getDrugYaoDan(param){
    return getRequest(
        'post',
        "/drug/yaodan",
        param,
        store.state.user.token,
    );
}
/**
用户管理
**/
//添加药品
export function getUsers(param){
    return getRequest(
        'post',
        "/users/list",
        param,
        store.state.user.token,
    );
}

